import {areaList} from '@vant/area-data';
import $ from '../common';
Page({
  data: {
    areaList,
    address: '',
    code:"140107"
  },
  reset(){//重新定位
    this.check((res => {
      this.getaddress()
    }))
  },
  getArea(val) {//确定
    const {values}=val.detail
    this.setData({
      address:values[2].name,
    })
    wx.setStorageSync('addresscode', values[2].code)
    wx.setStorageSync('address', values[2].name)
    $.navigateBack()
  },
  showArea(val) {//滑动
    // console.log('showArea-- ',val)
  },
  colseArea(e) {//关闭
    console.log('colseArea-- ',e)
  },
  check(ck) {//检查权限
    wx.getSetting({
      success: (res) => {
        // if (!res.authSetting['scope.userLocationBackground']) {
        if (!res.authSetting['scope.userLocation'] || res.authSetting['scope.userLocation'] == false) {
          console.log('没有授权');
          wx.authorize({ //授权弹框
            // scope: 'scope.userLocationBackground',
            scope: 'scope.userLocation',
            success: () => {
              console.log('授权弹框授权成功');
              ck && ck()
            },
            fail(err) { //拒绝后提示内容
              wx.showModal({
                title: '获取位置失败',
                content: '请先点击右上角位置允许获取当前位置',
                success(res) {
                  if (res.confirm) {
                    wx.openSetting({
                      success(res) {
                        console.log(res.authSetting);
                        // ck && ck()
                      }
                    })
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
            }
          })
        } else {
          console.log('已授权');
          ck && ck()
        }
      }
    })
  },
  getaddress() {//获取地理位置
    this.setData({
      address: '',
    })
    wx.getLocation({
      altitude: 'altitude',
      // isHighAccuracy: true,
      success:(res)=> {
        const latitude = res.latitude //纬度
        const longitude = res.longitude //经度
        this.setData({
          lnglat: longitude + ',' + latitude,
        })
        wx.request({
          url: 'https://restapi.amap.com/v3/geocode/regeo',
          data: {
            key: '6313d1bbd7f3ac72a5a13825a131f771',
            location: longitude + "," + latitude,
            extensions: "all",
            s: "rsx",
            sdkversion: "sdkversion",
            logversion: "logversion"
          },
          success: (ras) => {
            let gaoDaddress=ras.data.regeocode.addressComponent.district
            let adcode=ras.data.regeocode.addressComponent.adcode
            this.setData({
              address: gaoDaddress,
              addresscode:adcode
            })
            wx.setStorageSync('addresscode', adcode)
            wx.setStorageSync('address', gaoDaddress)
          },
          fail: fail => {
            this.setData({
              address: '定位失败'
            })
            console.log('高德定位失败');
          },
          complete: (all) => {}
        })
      },
      fail:(err)=>{
        console.log('微信定位失败');
        console.log(err);
      }
    })
  },
  onLoad(e) {
    wx.setNavigationBarTitle({title:'选择城市'})
    this.setData({
      addresscode:wx.getStorageSync('addresscode')||'',
      address:wx.getStorageSync('address')||''
    })
    if(!wx.getStorageSync('addresscode')){
      this.check((res => {
        this.getaddress()
      }))
    }
  },
  onShow() {},
})